package com.mxm.mirai.quartz;

import com.mxm.mirai.config.MXM_config;
import com.mxm.mirai.service.BotgroupsService;
import com.mxm.mirai.service.WeiborService;
import net.mamoe.mirai.Bot;
import net.mamoe.mirai.contact.Group;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

//@Component
public class Quartz_Weibo_Run extends QuartzJobBean {

    @Autowired
    BotgroupsService botgroupsService;
    @Autowired
    WeiborService weiborService;

    @Override
    protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
        SimpleDateFormat df2 = new SimpleDateFormat("HH:mm:ss");//设置日期格式
//        System.out.println(context.getJobDetail().getJobDataMap().getString("qq"));
        MXM_config.log.BotLog(null,"===================微博热任务运行中==================="+df2.format(new Date()),true);
        String text = weiborService.getWeibo();
        if (null != text && !"".equals(text)) {
            for (Bot bot : Bot.getBotInstances()) {
                for (Group group:bot.getGroups()){
                    if("1".equals(botgroupsService.QueryBotGroupsState(String.valueOf(bot.getId()),String.valueOf(group.getId()),"WeiboMonitor"))){
                        QQ_group_sendMessage(bot,group,text);
                    }
                }
            }
        }
    }
    /**
     * 获取
     * @param bot
     * @param group
     */
    public void QQ_group_sendMessage(Bot bot,Group group,String text){
        try {
            if (bot.isOnline()) {//在线则发送
                group.sendMessage("=====微博热搜=====\n"+text);
            }
        } catch (Exception e) {
            MXM_config.log.BotLog(String.valueOf(bot.getId()),"微博热任务运行中发送出错了",true);
        }
    }

}
